Load all required libraries.

library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5     v purrr   0.3.4
## v tibble  3.1.3     v dplyr   1.0.7
## v tidyr   1.1.3     v stringr 1.4.0
## v readr   2.0.0     v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(broom)

Read in raw data from RDS.

raw_data <- readRDS("./n1_n2_cleaned_cases.rds")

Make a few small modifications to names and data for visualizations.

final_data <- raw_data %>% mutate(log_copy_per_L = log10(mean_copy_num_L)) %>%
  rename(Facility = wrf) %>%
  mutate(Facility = recode(Facility, 
                           "NO" = "WRF A",
                           "MI" = "WRF B",
                           "CC" = "WRF C"))

Seperate the data by gene target to ease layering in the final plot

#make three data layers
only_positives <<- subset(final_data, (!is.na(final_data$Facility)))
only_n1 <- subset(only_positives, target == "N1")
only_n2 <- subset(only_positives, target == "N2")
only_background <<-final_data %>% 
  select(c(date, cases_cum_clarke, new_cases_clarke, X7_day_ave_clarke)) %>%
  group_by(date) %>% summarise_if(is.numeric, mean)

#specify fun colors
background_color <- "#7570B3"
seven_day_ave_color <- "#E6AB02"
marker_colors <- c("N1" = '#1B9E77',"N2" ='#D95F02')
#remove facilty C for now
#only_n1 <- only_n1[!(only_n1$Facility == "WRF C"),]
#only_n2 <- only_n2[!(only_n2$Facility == "WRF C"),]

only_n1 <- only_n1[!(only_n1$Facility == "WRF A" & only_n1$date == "2020-11-02"), ]
only_n2 <- only_n2[!(only_n2$Facility == "WRF A" & only_n2$date == "2020-11-02"), ]

Build the main plot

      #first layer is the background epidemic curve
        p1 <- only_background %>%
              plotly::plot_ly() %>%
              plotly::add_trace(x = ~date, y = ~new_cases_clarke, 
                                type = "bar", 
                                hoverinfo = "text",
                                text = ~paste('</br> Date: ', date,
                                                     '</br> Daily Cases: ', new_cases_clarke),
                                alpha = 0.5,
                                name = "Daily Reported Cases",
                                color = background_color,
                                colors = background_color,
                                showlegend = FALSE) %>%
            layout(yaxis = list(title = "Clarke County Daily Cases", showline=TRUE)) %>%
            layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
        
        #renders the main plot layer two as seven day moving average
        p1 <- p1 %>% plotly::add_trace(x = ~date, y = ~X7_day_ave_clarke, 
                             type = "scatter",
                             mode = "lines",
                             hoverinfo = "text",
                            text = ~paste('</br> Date: ', date,
                                                     '</br> Seven-Day Moving Average: ', X7_day_ave_clarke),
                             name = "Seven Day Moving Average Athens",
                             line = list(color = seven_day_ave_color),
                             showlegend = FALSE)
      

        
        #renders the main plot layer three as positive target hits
        
        p2 <- plotly::plot_ly() %>%
          plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
                                       type = "scatter",
                                       mode = "markers",
                                       hoverinfo = "text",
                                       text = ~paste('</br> Date: ', date,
                                                     '</br> Facility: ', Facility,
                                                     '</br> Target: ', target,
                                                     '</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
                                       data = only_n1,
                                       symbol = ~Facility,
                                       marker = list(color = '#1B9E77', size = 8, opacity = 0.65),
                                       showlegend = FALSE) %>%
          plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
                                       type = "scatter",
                                       mode = "markers",
                                       hoverinfo = "text",
                                       text = ~paste('</br> Date: ', date,
                                                     '</br> Facility: ', Facility,
                                                     '</br> Target: ', target,
                                                     '</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
                                       data = only_n2,
                                       symbol = ~Facility,
                                       marker = list(color = '#D95F02', size = 8, opacity = 0.65),
                                       showlegend = FALSE) %>%
            layout(yaxis = list(title = "SARS CoV-2 Copies/L", 
                                 showline = TRUE,
                                 type = "log",
                                 dtick = 1,
                                 automargin = TRUE)) %>%
            layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
        
        #adds the limit of detection dashed line
        p2 <- p2 %>% plotly::add_segments(x = as.Date("2020-03-14"), 
                                          xend = ~max(date + 10), 
                                          y = 3571.429, yend = 3571.429,
                                          opacity = 0.35,
                                          line = list(color = "black", dash = "dash")) %>%
          layout(annotations = list(x = as.Date("2020-03-28"), y = 3.8, xref = "x", yref = "y", 
                                    text = "Limit of Detection", showarrow = FALSE))

        

        p1
        p2

Combine the two main plot pieces as a subplot

#seperate n1 and n2 frames by site
#n1
wrf_a_only_n1 <- subset(only_n1, Facility == "WRF A")
wrf_b_only_n1 <- subset(only_n1, Facility == "WRF B")
wrf_c_only_n1 <- subset(only_n1, Facility == "WRF C")

#n2
wrf_a_only_n2 <- subset(only_n2, Facility == "WRF A")
wrf_b_only_n2 <- subset(only_n2, Facility == "WRF B")
wrf_c_only_n2 <- subset(only_n2, Facility == "WRF C")


#rejoin the old data frames then seperate in to averages for each plant. 
wrfa_both <- full_join(wrf_a_only_n1, wrf_a_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke", "X7_day_ave_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "log_copy_per_L")
wrfb_both <- full_join(wrf_b_only_n1, wrf_b_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke", "X7_day_ave_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "log_copy_per_L")
wrfc_both <- full_join(wrf_c_only_n1, wrf_c_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke", "X7_day_ave_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "log_copy_per_L")
#get max date
maxdate <- max(wrfa_both$date)
mindate <- min(wrfa_both$date)

Build loess smoothing figures figures

This makes the individual plots

#**************************************WRF A PLOT**********************************************
#add trendlines 
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_botha <- ggplot(wrfa_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_botha<<-..y..), method = "loess", color = '#1B9E77', 
              span = 0.3, n = 590)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_botha
## `geom_smooth()` using formula 'y ~ x'

fit_botha
##   [1] 12.99571 12.99382 12.99198 12.99019 12.98845 12.98674 12.98507 12.98342
##   [9] 12.98180 12.98019 12.97859 12.97699 12.97540 12.97380 12.97219 12.97056
##  [17] 12.96891 12.96724 12.96553 12.96378 12.96199 12.96015 12.95826 12.95630
##  [25] 12.95429 12.95220 12.95003 12.94779 12.94545 12.94304 12.94056 12.93803
##  [33] 12.93544 12.93280 12.93013 12.92742 12.92468 12.92192 12.91915 12.91637
##  [41] 12.91359 12.91081 12.90804 12.90529 12.90256 12.89985 12.89719 12.89456
##  [49] 12.89199 12.88946 12.88700 12.88461 12.88228 12.88004 12.87788 12.87581
##  [57] 12.87385 12.87187 12.86980 12.86763 12.86537 12.86304 12.86064 12.85818
##  [65] 12.85567 12.85312 12.85053 12.84792 12.84530 12.84266 12.84003 12.83740
##  [73] 12.83479 12.83221 12.82967 12.82717 12.82472 12.82233 12.82002 12.81778
##  [81] 12.81563 12.81357 12.81162 12.80978 12.80807 12.80648 12.80503 12.80373
##  [89] 12.80259 12.80161 12.80081 12.80019 12.79927 12.79761 12.79528 12.79233
##  [97] 12.78883 12.78484 12.78042 12.77564 12.77055 12.76522 12.75971 12.75409
## [105] 12.74841 12.74274 12.73714 12.73167 12.72640 12.72138 12.71668 12.71236
## [113] 12.70848 12.70511 12.70231 12.70013 12.69865 12.69792 12.69800 12.69897
## [121] 12.70081 12.70345 12.70683 12.71088 12.71554 12.72077 12.72649 12.73264
## [129] 12.73918 12.74603 12.75313 12.76043 12.76787 12.77539 12.78292 12.79041
## [137] 12.79780 12.80502 12.81202 12.81874 12.82512 12.83109 12.83874 12.84983
## [145] 12.86381 12.88013 12.89824 12.91759 12.93764 12.95784 12.97763 12.99647
## [153] 13.01382 13.02912 13.04182 13.05138 13.06026 13.07112 13.08371 13.09777
## [161] 13.11305 13.12931 13.14629 13.16374 13.18141 13.19904 13.21639 13.23320
## [169] 13.24922 13.26420 13.27789 13.29004 13.30039 13.30870 13.31470 13.32014
## [177] 13.32680 13.33453 13.34315 13.35250 13.36243 13.37277 13.38335 13.39402
## [185] 13.40460 13.41495 13.42488 13.43425 13.44289 13.45064 13.45732 13.46279
## [193] 13.46688 13.46942 13.47024 13.46920 13.46613 13.46085 13.45343 13.44413
## [201] 13.43309 13.42045 13.40638 13.39100 13.37448 13.35696 13.33859 13.31952
## [209] 13.29988 13.27984 13.25953 13.23911 13.21872 13.19851 13.17863 13.15922
## [217] 13.14044 13.11929 13.09318 13.06290 13.02921 12.99290 12.95472 12.91547
## [225] 12.87590 12.83680 12.79893 12.76308 12.73000 12.70049 12.67531 12.65042
## [233] 12.62168 12.58966 12.55494 12.51811 12.47974 12.44043 12.40074 12.36127
## [241] 12.32260 12.28530 12.24995 12.21715 12.18747 12.16150 12.13980 12.12102
## [249] 12.10330 12.08656 12.07071 12.05565 12.04130 12.02757 12.01435 12.00157
## [257] 11.98912 11.97692 11.96488 11.95290 11.94089 11.93045 11.92303 11.91830
## [265] 11.91594 11.91562 11.91702 11.91982 11.92369 11.92830 11.93335 11.93849
## [273] 11.94342 11.94780 11.95130 11.95362 11.95442 11.95338 11.95017 11.94448
## [281] 11.93829 11.93362 11.93023 11.92785 11.92622 11.92508 11.92418 11.92326
## [289] 11.92205 11.92029 11.91774 11.91413 11.90919 11.90268 11.89548 11.88863
## [297] 11.88206 11.87571 11.86951 11.86341 11.85734 11.85124 11.84504 11.83868
## [305] 11.83210 11.82524 11.81804 11.81042 11.80234 11.79372 11.78300 11.76903
## [313] 11.75235 11.73351 11.71305 11.69153 11.66949 11.64747 11.62603 11.60571
## [321] 11.58706 11.57062 11.55694 11.54658 11.53642 11.52329 11.50760 11.48974
## [329] 11.47013 11.44917 11.42726 11.40482 11.38224 11.35993 11.33830 11.31775
## [337] 11.29869 11.28153 11.26666 11.25451 11.24546 11.23992 11.23831 11.23902
## [345] 11.24021 11.24193 11.24422 11.24713 11.25072 11.25502 11.26009 11.26597
## [353] 11.27272 11.28038 11.28899 11.29861 11.30929 11.32220 11.33832 11.35735
## [361] 11.37904 11.40309 11.42922 11.45717 11.48665 11.51739 11.54910 11.58151
## [369] 11.61433 11.64730 11.68014 11.71256 11.74428 11.77504 11.80454 11.83252
## [377] 11.85869 11.88277 11.90850 11.93922 11.97410 12.01229 12.05296 12.09527
## [385] 12.13839 12.18147 12.22368 12.26418 12.30213 12.33670 12.36704 12.39232
## [393] 12.41658 12.44404 12.47418 12.50648 12.54038 12.57537 12.61092 12.64648
## [401] 12.68153 12.71554 12.74798 12.77831 12.80600 12.83051 12.85133 12.86792
## [409] 12.88207 12.89592 12.90937 12.92236 12.93481 12.94665 12.95780 12.96818
## [417] 12.97773 12.98636 12.99400 13.00058 13.00602 13.01024 13.01222 13.01115
## [425] 13.00733 13.00103 12.99255 12.98217 12.97018 12.95687 12.94252 12.92742
## [433] 12.91185 12.89611 12.88048 12.86525 12.85070 12.83711 12.82479 12.81401
## [441] 12.80506 12.79492 12.78081 12.76335 12.74318 12.72091 12.69719 12.67263
## [449] 12.64787 12.62353 12.60024 12.57863 12.55933 12.54296 12.53016 12.51823
## [457] 12.50428 12.48862 12.47155 12.45338 12.43443 12.41501 12.39542 12.37597
## [465] 12.35698 12.33874 12.32159 12.30581 12.29173 12.27965 12.26988 12.26105
## [473] 12.25166 12.24187 12.23182 12.22168 12.21160 12.20171 12.19219 12.18317
## [481] 12.17481 12.16726 12.16067 12.15520 12.15100 12.14781 12.14528 12.14339
## [489] 12.14212 12.14144 12.14134 12.14179 12.14277 12.14427 12.14626 12.14872
## [497] 12.15163 12.15496 12.15871 12.16284 12.16734 12.17219 12.17736 12.18283
## [505] 12.19003 12.20013 12.21272 12.22743 12.24384 12.26158 12.28023 12.29942
## [513] 12.31873 12.33779 12.35619 12.37353 12.38944 12.40350 12.41533 12.42452
## [521] 12.43298 12.44274 12.45361 12.46542 12.47800 12.49115 12.50471 12.51849
## [529] 12.53231 12.54600 12.55938 12.57227 12.58448 12.59585 12.60619 12.61532
## [537] 12.62306 12.62924 12.63367 12.63721 12.64079 12.64438 12.64794 12.65145
## [545] 12.65486 12.65814 12.66125 12.66416 12.66684 12.66925 12.67136 12.67313
## [553] 12.67452 12.67550 12.67604 12.67610 12.67565 12.67465 12.67307 12.67087
## [561] 12.66824 12.66538 12.66228 12.65892 12.65527 12.65134 12.64709 12.64251
## [569] 12.63759 12.63230 12.62664 12.62058 12.61411 12.60721 12.59986 12.59205
## [577] 12.58363 12.57447 12.56463 12.55414 12.54305 12.53140 12.51923 12.50659
## [585] 12.49352 12.48005 12.46624 12.45213 12.43775 12.42315
#assign fits to a vector
both_trenda <- fit_botha

#extract y min and max for each
limits_botha <- ggplot_build(extract_botha)$data
## `geom_smooth()` using formula 'y ~ x'
limits_botha <- as.data.frame(limits_botha)
both_ymina <- limits_botha$ymin
both_ymaxa <- limits_botha$ymax

#reassign dataframes (just to be safe)
work_botha <- wrfa_both

#fill in missing dates to smooth fits
work_botha <- work_botha %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_botha <- work_botha$date

#create a new smooth dataframe to layer
smooth_frame_botha <- data.frame(date_vec_botha, both_trenda, both_ymina, both_ymaxa)
#WRF A
#plot smooth frames
p_wrf_a <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_botha, y = ~both_trenda,
                    data = smooth_frame_botha,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_botha,
                                  '</br> Median Log Copies: ', round(both_trenda, digits = 2)),
                    line = list(color = '#1B9E77', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_botha, ymin = ~both_ymina, ymax = ~both_ymaxa,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_botha, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxa, digits = 2),
                                  '</br> Min Log Copies: ', round(both_ymina, digits = 2)),
                    name = "",
                    fillcolor = '#1B9E77',
                    line = list(color = '#1B9E77')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF A") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfa_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#1B9E77', size = 6, opacity = 0.65))

p_wrf_a
save(p_wrf_a, file = "./plotly_objs/p_wrf_a.rda")
#**************************************WRF B PLOT**********************************************
#add trendlines 
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothb <- ggplot(wrfb_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_bothb<<-..y..), method = "loess", color = '#D95F02', 
              span = 0.3, n = 590)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothb
## `geom_smooth()` using formula 'y ~ x'

fit_bothb
##   [1] 12.61263 12.60874 12.60496 12.60128 12.59769 12.59420 12.59080 12.58748
##   [9] 12.58425 12.58110 12.57802 12.57501 12.57208 12.56920 12.56639 12.56364
##  [17] 12.56094 12.55829 12.55569 12.55312 12.55060 12.54812 12.54566 12.54324
##  [25] 12.54084 12.53845 12.53609 12.53374 12.53140 12.52907 12.52673 12.52440
##  [33] 12.52209 12.51979 12.51750 12.51525 12.51302 12.51082 12.50867 12.50655
##  [41] 12.50449 12.50247 12.50051 12.49861 12.49678 12.49501 12.49332 12.49171
##  [49] 12.49019 12.48875 12.48740 12.48615 12.48500 12.48396 12.48302 12.48221
##  [57] 12.48151 12.48093 12.48049 12.48018 12.48000 12.47997 12.48008 12.48035
##  [65] 12.48072 12.48115 12.48164 12.48219 12.48281 12.48350 12.48426 12.48509
##  [73] 12.48600 12.48700 12.48807 12.48923 12.49048 12.49181 12.49325 12.49477
##  [81] 12.49640 12.49813 12.49996 12.50190 12.50395 12.50611 12.50839 12.51078
##  [89] 12.51330 12.51594 12.51870 12.52160 12.52462 12.52778 12.53108 12.53452
##  [97] 12.53809 12.54182 12.54569 12.54951 12.55310 12.55647 12.55965 12.56267
## [105] 12.56555 12.56830 12.57096 12.57355 12.57608 12.57858 12.58108 12.58360
## [113] 12.58616 12.58879 12.59150 12.59432 12.59727 12.60039 12.60368 12.60717
## [121] 12.61089 12.61486 12.61910 12.62363 12.62848 12.63368 12.63953 12.64629
## [129] 12.65383 12.66206 12.67086 12.68012 12.68974 12.69960 12.70961 12.71964
## [137] 12.72960 12.73937 12.74884 12.75791 12.76647 12.77441 12.78401 12.79726
## [145] 12.81357 12.83235 12.85302 12.87500 12.89769 12.92051 12.94287 12.96420
## [153] 12.98390 13.00139 13.01609 13.02740 13.03851 13.05273 13.06965 13.08891
## [161] 13.11009 13.13282 13.15670 13.18133 13.20634 13.23132 13.25590 13.27967
## [169] 13.30224 13.32323 13.34224 13.35889 13.37278 13.38352 13.39073 13.39610
## [177] 13.40158 13.40709 13.41256 13.41791 13.42307 13.42797 13.43254 13.43670
## [185] 13.44039 13.44352 13.44602 13.44783 13.44887 13.44907 13.44835 13.44665
## [193] 13.44389 13.43999 13.43489 13.42850 13.42077 13.41162 13.39994 13.38492
## [201] 13.36689 13.34618 13.32311 13.29803 13.27126 13.24314 13.21400 13.18417
## [209] 13.15398 13.12377 13.09386 13.06460 13.03630 13.00931 12.98396 12.96057
## [217] 12.93948 12.91683 12.88908 12.85709 12.82169 12.78372 12.74403 12.70346
## [225] 12.66284 12.62303 12.58487 12.54918 12.51683 12.48864 12.46547 12.44383
## [233] 12.41995 12.39420 12.36696 12.33860 12.30950 12.28003 12.25058 12.22151
## [241] 12.19320 12.16604 12.14039 12.11663 12.09514 12.07629 12.06046 12.04737
## [249] 12.03633 12.02709 12.01940 12.01300 12.00763 12.00306 11.99903 11.99527
## [257] 11.99155 11.98761 11.98319 11.97805 11.97193 11.96711 11.96577 11.96752
## [265] 11.97200 11.97881 11.98758 11.99792 12.00945 12.02179 12.03456 12.04737
## [273] 12.05985 12.07162 12.08228 12.09146 12.09878 12.10386 12.10631 12.10575
## [281] 12.10525 12.10776 12.11276 12.11969 12.12803 12.13723 12.14676 12.15608
## [289] 12.16464 12.17193 12.17738 12.18048 12.18067 12.17743 12.17155 12.16431
## [297] 12.15585 12.14634 12.13593 12.12477 12.11302 12.10082 12.08834 12.07573
## [305] 12.06315 12.05074 12.03866 12.02707 12.01612 12.00597 11.99396 11.97779
## [313] 11.95815 11.93572 11.91117 11.88518 11.85843 11.83160 11.80538 11.78043
## [321] 11.75743 11.73707 11.72003 11.70698 11.69455 11.67915 11.66120 11.64110
## [329] 11.61925 11.59605 11.57191 11.54724 11.52242 11.49787 11.47399 11.45119
## [337] 11.42986 11.41040 11.39323 11.37874 11.36734 11.35943 11.35542 11.35359
## [345] 11.35202 11.35081 11.35003 11.34978 11.35014 11.35119 11.35303 11.35573
## [353] 11.35939 11.36408 11.36991 11.37694 11.38527 11.39600 11.40997 11.42691
## [361] 11.44652 11.46852 11.49263 11.51856 11.54603 11.57476 11.60446 11.63485
## [369] 11.66564 11.69655 11.72729 11.75759 11.78715 11.81569 11.84293 11.86859
## [377] 11.89238 11.91401 11.93701 11.96458 11.99598 12.03043 12.06718 12.10548
## [385] 12.14456 12.18367 12.22204 12.25893 12.29357 12.32520 12.35307 12.37641
## [393] 12.39844 12.42262 12.44859 12.47599 12.50445 12.53362 12.56313 12.59261
## [401] 12.62172 12.65007 12.67733 12.70311 12.72706 12.74881 12.76801 12.78429
## [409] 12.79953 12.81568 12.83249 12.84969 12.86705 12.88429 12.90117 12.91744
## [417] 12.93283 12.94709 12.95997 12.97120 12.98055 12.98775 12.99294 12.99649
## [425] 12.99851 12.99911 12.99838 12.99644 12.99340 12.98934 12.98439 12.97863
## [433] 12.97219 12.96517 12.95766 12.94978 12.94162 12.93330 12.92492 12.91659
## [441] 12.90840 12.89810 12.88374 12.86596 12.84536 12.82256 12.79817 12.77282
## [449] 12.74712 12.72168 12.69712 12.67406 12.65311 12.63489 12.62001 12.60510
## [457] 12.58674 12.56543 12.54170 12.51608 12.48908 12.46122 12.43302 12.40500
## [465] 12.37770 12.35161 12.32728 12.30521 12.28592 12.26995 12.25781 12.24646
## [473] 12.23283 12.21737 12.20054 12.18281 12.16461 12.14641 12.12867 12.11184
## [481] 12.09638 12.08274 12.07138 12.06276 12.05733 12.05385 12.05077 12.04811
## [489] 12.04591 12.04418 12.04295 12.04222 12.04204 12.04241 12.04336 12.04492
## [497] 12.04709 12.04992 12.05341 12.05758 12.06247 12.06809 12.07447 12.08162
## [505] 12.09126 12.10475 12.12159 12.14125 12.16324 12.18704 12.21214 12.23803
## [513] 12.26420 12.29014 12.31534 12.33928 12.36147 12.38138 12.39851 12.41234
## [521] 12.42529 12.43996 12.45612 12.47357 12.49209 12.51145 12.53145 12.55186
## [529] 12.57246 12.59305 12.61340 12.63329 12.65252 12.67085 12.68808 12.70398
## [537] 12.71835 12.73095 12.74158 12.75132 12.76136 12.77164 12.78211 12.79271
## [545] 12.80338 12.81408 12.82473 12.83529 12.84570 12.85591 12.86585 12.87548
## [553] 12.88473 12.89355 12.90189 12.90968 12.91687 12.92341 12.92924 12.93431
## [561] 12.93888 12.94328 12.94747 12.95145 12.95518 12.95866 12.96186 12.96476
## [569] 12.96735 12.96960 12.97150 12.97302 12.97415 12.97488 12.97517 12.97501
## [577] 12.97420 12.97261 12.97028 12.96728 12.96364 12.95943 12.95471 12.94951
## [585] 12.94391 12.93794 12.93167 12.92515 12.91843 12.91156
#assign fits to a vector
both_trendb <- fit_bothb

#extract y min and max for each
limits_bothb <- ggplot_build(extract_bothb)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothb <- as.data.frame(limits_bothb)
both_yminb <- limits_bothb$ymin
both_ymaxb <- limits_bothb$ymax

#reassign dataframes (just to be safe)
work_bothb <- wrfb_both

#fill in missing dates to smooth fits
work_bothb <- work_bothb %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothb <- work_bothb$date

#create a new smooth dataframe to layer
smooth_frame_bothb <- data.frame(date_vec_bothb, both_trendb, both_yminb, both_ymaxb)
#WRF B
#plot smooth frames
p_wrf_b <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_bothb, y = ~both_trendb,
                    data = smooth_frame_bothb,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothb,
                                  '</br> Median Log Copies: ', round(both_trendb, digits = 2)),
                    line = list(color = '#D95F02', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothb, ymin = ~both_yminb, ymax = ~both_ymaxb,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothb, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxb, digits = 2),
                                  '</br> Min Log Copies: ', round(both_yminb, digits = 2)),
                    name = "",
                    fillcolor = '#D95F02',
                    line = list(color = '#D95F02')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF B") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfb_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#D95F02', size = 6, opacity = 0.65))

p_wrf_b
save(p_wrf_b, file = "./plotly_objs/p_wrf_b.rda")

#**************************************WRF C PLOT********************************************** #add trendlines #extract data from geom_smooth # *********************************span 0.6*********************************** #*****************Must always update the n = TOTAL NUMBER OF DAYS*************************

extract_bothc <- ggplot(wrfc_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_bothc<<-..y..), method = "loess", color = '#E7298A', 
              span = 0.3, n = 590)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothc
## `geom_smooth()` using formula 'y ~ x'

fit_bothc
##   [1] 11.98389 11.98087 11.97795 11.97512 11.97238 11.96971 11.96711 11.96457
##   [9] 11.96209 11.95966 11.95727 11.95492 11.95260 11.95031 11.94803 11.94576
##  [17] 11.94349 11.94122 11.93895 11.93665 11.93433 11.93198 11.92960 11.92717
##  [25] 11.92470 11.92216 11.91957 11.91690 11.91416 11.91134 11.90843 11.90542
##  [33] 11.90230 11.89908 11.89575 11.89229 11.88871 11.88504 11.88129 11.87746
##  [41] 11.87358 11.86964 11.86567 11.86167 11.85766 11.85364 11.84963 11.84564
##  [49] 11.84168 11.83776 11.83389 11.83009 11.82636 11.82272 11.81918 11.81575
##  [57] 11.81244 11.80926 11.80623 11.80335 11.80064 11.79810 11.79576 11.79361
##  [65] 11.79138 11.78878 11.78583 11.78257 11.77903 11.77522 11.77119 11.76696
##  [73] 11.76256 11.75801 11.75335 11.74860 11.74380 11.73896 11.73412 11.72931
##  [81] 11.72455 11.71988 11.71531 11.71089 11.70663 11.70258 11.69874 11.69516
##  [89] 11.69186 11.68887 11.68622 11.68394 11.68205 11.68058 11.67957 11.67903
##  [97] 11.67901 11.67952 11.68059 11.68179 11.68267 11.68329 11.68369 11.68392
## [105] 11.68402 11.68404 11.68402 11.68401 11.68405 11.68419 11.68448 11.68496
## [113] 11.68567 11.68667 11.68800 11.68970 11.69181 11.69439 11.69749 11.70113
## [121] 11.70538 11.71028 11.71586 11.72219 11.72930 11.73724 11.74717 11.75999
## [129] 11.77534 11.79286 11.81219 11.83297 11.85484 11.87745 11.90044 11.92344
## [137] 11.94610 11.96805 11.98895 12.00842 12.02612 12.04168 12.05849 12.07972
## [145] 12.10465 12.13258 12.16280 12.19461 12.22730 12.26016 12.29249 12.32358
## [153] 12.35273 12.37923 12.40237 12.42144 12.43978 12.46095 12.48458 12.51030
## [161] 12.53774 12.56654 12.59632 12.62672 12.65738 12.68792 12.71797 12.74718
## [169] 12.77517 12.80157 12.82601 12.84814 12.86758 12.88396 12.89691 12.90837
## [177] 12.92043 12.93295 12.94580 12.95885 12.97198 12.98504 12.99791 13.01046
## [185] 13.02255 13.03407 13.04486 13.05481 13.06378 13.07164 13.07827 13.08352
## [193] 13.08727 13.08939 13.08975 13.08821 13.08465 13.07893 13.07030 13.05830
## [201] 13.04325 13.02545 13.00522 12.98288 12.95874 12.93312 12.90632 12.87867
## [209] 12.85047 12.82203 12.79369 12.76574 12.73850 12.71229 12.68742 12.66420
## [217] 12.64294 12.61926 12.58924 12.55391 12.51432 12.47148 12.42645 12.38026
## [225] 12.33394 12.28852 12.24505 12.20456 12.16809 12.13666 12.11132 12.08813
## [233] 12.06272 12.03543 12.00665 11.97672 11.94602 11.91492 11.88377 11.85293
## [241] 11.82279 11.79369 11.76600 11.74009 11.71631 11.69505 11.67665 11.66053
## [249] 11.64575 11.63216 11.61959 11.60790 11.59693 11.58652 11.57653 11.56679
## [257] 11.55715 11.54746 11.53756 11.52729 11.51651 11.50710 11.50082 11.49735
## [265] 11.49634 11.49745 11.50034 11.50467 11.51009 11.51628 11.52288 11.52957
## [273] 11.53599 11.54181 11.54670 11.55030 11.55228 11.55229 11.55001 11.54508
## [281] 11.54021 11.53801 11.53804 11.53984 11.54297 11.54698 11.55143 11.55585
## [289] 11.55981 11.56286 11.56454 11.56442 11.56203 11.55694 11.54934 11.53991
## [297] 11.52890 11.51656 11.50313 11.48885 11.47399 11.45878 11.44348 11.42833
## [305] 11.41358 11.39947 11.38627 11.37421 11.36354 11.35451 11.34363 11.32781
## [313] 11.30788 11.28469 11.25906 11.23184 11.20385 11.17595 11.14896 11.12373
## [321] 11.10109 11.08188 11.06693 11.05708 11.04943 11.04064 11.03092 11.02047
## [329] 11.00949 10.99818 10.98674 10.97538 10.96430 10.95370 10.94378 10.93475
## [337] 10.92680 10.92014 10.91498 10.91150 10.90992 10.91044 10.91325 10.91855
## [345] 10.92619 10.93591 10.94741 10.96043 10.97468 10.98989 11.00578 11.02207
## [353] 11.03848 11.05474 11.07057 11.08568 11.09980 11.11475 11.13237 11.15241
## [361] 11.17462 11.19877 11.22461 11.25189 11.28038 11.30982 11.33998 11.37060
## [369] 11.40146 11.43229 11.46286 11.49293 11.52225 11.55057 11.57766 11.60326
## [377] 11.62714 11.64905 11.67153 11.69693 11.72476 11.75451 11.78566 11.81772
## [385] 11.85018 11.88253 11.91426 11.94488 11.97386 12.00072 12.02493 12.04600
## [393] 12.06616 12.08780 12.11064 12.13443 12.15890 12.18376 12.20876 12.23362
## [401] 12.25807 12.28185 12.30469 12.32631 12.34645 12.36483 12.38119 12.39526
## [409] 12.40861 12.42283 12.43768 12.45294 12.46837 12.48374 12.49881 12.51336
## [417] 12.52714 12.53993 12.55150 12.56160 12.57001 12.57650 12.58085 12.58314
## [425] 12.58353 12.58223 12.57940 12.57523 12.56990 12.56358 12.55647 12.54875
## [433] 12.54059 12.53217 12.52368 12.51530 12.50721 12.49959 12.49262 12.48649
## [441] 12.48137 12.47537 12.46672 12.45582 12.44307 12.42886 12.41359 12.39765
## [449] 12.38143 12.36534 12.34976 12.33510 12.32174 12.31009 12.30053 12.29122
## [457] 12.28020 12.26771 12.25399 12.23928 12.22383 12.20787 12.19165 12.17541
## [465] 12.15938 12.14381 12.12895 12.11503 12.10229 12.09097 12.08132 12.07175
## [473] 12.06069 12.04843 12.03525 12.02143 12.00726 11.99301 11.97897 11.96542
## [481] 11.95265 11.94093 11.93055 11.92178 11.91492 11.90878 11.90209 11.89496
## [489] 11.88752 11.87989 11.87219 11.86454 11.85706 11.84988 11.84311 11.83687
## [497] 11.83130 11.82651 11.82261 11.81974 11.81801 11.81755 11.81847 11.82090
## [505] 11.82520 11.83148 11.83952 11.84908 11.85992 11.87180 11.88450 11.89777
## [513] 11.91139 11.92512 11.93871 11.95195 11.96459 11.97639 11.98713 11.99656
## [521] 12.00613 12.01730 12.02988 12.04370 12.05856 12.07428 12.09067 12.10755
## [529] 12.12473 12.14202 12.15925 12.17621 12.19273 12.20862 12.22370 12.23778
## [537] 12.25066 12.26218 12.27213 12.28139 12.29094 12.30072 12.31071 12.32088
## [545] 12.33119 12.34160 12.35209 12.36261 12.37314 12.38363 12.39406 12.40439
## [553] 12.41458 12.42461 12.43443 12.44402 12.45333 12.46234 12.47100 12.47929
## [561] 12.48739 12.49548 12.50357 12.51162 12.51963 12.52758 12.53545 12.54324
## [569] 12.55092 12.55849 12.56592 12.57320 12.58033 12.58727 12.59402 12.60057
## [577] 12.60675 12.61245 12.61771 12.62256 12.62706 12.63125 12.63515 12.63883
## [585] 12.64232 12.64565 12.64888 12.65204 12.65518 12.65834
#assign fits to a vector
both_trendc <- fit_bothc

#extract y min and max for each
limits_bothc <- ggplot_build(extract_bothc)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothc <- as.data.frame(limits_bothc)
both_yminc <- limits_bothc$ymin
both_ymaxc <- limits_bothc$ymax

#reassign dataframes (just to be safe)
work_bothc <- wrfc_both

#fill in missing dates to smooth fits
work_bothc <- work_bothc %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothc <- work_bothc$date

#create a new smooth dataframe to layer
smooth_frame_bothc <- data.frame(date_vec_bothc, both_trendc, both_yminc, both_ymaxc)
#WRF C
#plot smooth frames
p_wrf_c <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_bothc, y = ~both_trendc,
                    data = smooth_frame_bothc,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothc,
                                  '</br> Median Log Copies: ', round(both_trendc, digits = 2)),
                    line = list(color = '#E7298A', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothc, ymin = ~both_yminc, ymax = ~both_ymaxc,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothc, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxc, digits = 2),
                                  '</br> Min Log Copies: ', round(both_yminc, digits = 2)),
                    name = "",
                    fillcolor = '#E7298A',
                    line = list(color = '#E7298A')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF C") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfc_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#E7298A', size = 6, opacity = 0.65))

p_wrf_c
save(p_wrf_c, file = "./plotly_objs/p_wrf_c.rda")
save(wrfa_both, file = "./plotly_objs/wrfa_both.rda")
save(wrfb_both, file = "./plotly_objs/wrfb_both.rda")
save(wrfc_both, file = "./plotly_objs/wrfc_both.rda")
save(date_vec_botha, file = "./plotly_objs/date_vec_botha.rda")
save(date_vec_bothb, file = "./plotly_objs/date_vec_bothb.rda")
save(date_vec_bothc, file = "./plotly_objs/date_vec_bothc.rda")
save(both_ymina, file = "./plotly_objs/both_ymina.rda")
save(both_ymaxa, file = "./plotly_objs/both_ymaxa.rda")

save(both_yminb, file = "./plotly_objs/both_yminb.rda")
save(both_ymaxb, file = "./plotly_objs/both_ymaxb.rda")

save(both_yminc, file = "./plotly_objs/both_yminc.rda")
save(both_ymaxc, file = "./plotly_objs/both_ymaxc.rda")